import Vue from 'vue'
import VueRouter from 'vue-router'

// 解决导航栏或者底部导航tabBar中的vue-router在3.0版本以上频繁点击菜单报错的问题。
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Manager',
    component: () => import('../views/Manager.vue'),
    redirect: '/home', // 路由重定向
    children: [
      { path: '403', name: 'Auth', meta: {name: '无权限'}, component: () => import('../views/manager/Auth.vue') },
      { path: 'home', name: 'Home', meta: {name: '系统首页'}, component: () => import('../views/manager/Home') },
      { path: 'user', name: 'User', meta: {name: '用户信息'}, component: () => import('../views/manager/User') },
      { path: 'person', name: 'Person', meta: {name: '个人信息'}, component: () => import('../views/manager/Person') },
      { path: 'password', name: 'Password', meta: {name: '修改密码'}, component: () => import('../views/manager/Password') },
      { path: 'news', name: 'News', meta: {name: '新闻信息'}, component: () => import('../views/manager/News') },
      { path: 'newsDetail', name: 'NewsDetail', meta: {name: '新闻详情页'}, component: () => import('../views/manager/NewsDetail') },
      { path: 'notice', name: 'Notice', meta: {name: '系统公告'}, component: () => import('../views/manager/Notice') },
      { path: 'logs', name: 'Logs', meta: {name: '系统日志'}, component: () => import('../views/manager/Logs') },
      { path: 'chargingDevice', name: 'ChargingDevice', meta: {name: '充电宝信息'}, component: () => import('../views/manager/ChargingDevice') },
      { path: 'deviceType', name: 'DeviceType', meta: {name: '充电桩设备'}, component: () => import('../views/manager/DeviceType') },
      { path: 'address', name: 'Address', meta: {name: '设备地址'}, component: () => import('../views/manager/Address') },
      { path: 'rentRecord', name: 'RentRecord', meta: {name: '租借记录'}, component: () => import('../views/manager/RentRecord') },
      { path: 'userOrder', name: 'UserOrder', meta: {name: '用户订单'}, component: () => import('../views/manager/UserOrder') },
      { path: 'activity', name: 'Activity', meta: {name: '活动'}, component: () => import('../views/manager/Activity') },
      { path: '/test2', name: 'test2', meta: {name: 'CeShi'}, component: () => import('../views/test2.vue') },
    ]
  },
  { path: '/element', name: 'Element', meta: {name: '测试'}, component: () => import('../views/Element.vue' ) },
  { path: '/login', name: 'Login', meta: {name: '登录'}, component: () => import('../views/Login.vue') },
  { path: '/register', name: 'Register', meta: {name: '注册'}, component: () => import('../views/Register.vue') },
  { path: '*', name: '404', meta: {name: '无法访问'}, component: () => import('../views/404.vue') },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // to 是到达的路由信息
  // from 是来源的信息
  // next 帮助跳转路由的函数
  let adminPaths = ['/user']
  let user = JSON.parse(localStorage.getItem('power-user') || '{}')
  if (user.role !== '管理员' && adminPaths.includes(to.path)){
    // 如果当前登录的用户不是管理员，然后当前的到达路径是管理员才能访问的路径，那么这个时候就跳转到一个没有权限的页面
    next('/403')
  }else {
    next()
  }
})

export default router
